package com.fruit.service.account;

import com.fruit.pojo.push.Message;
import com.fruit.pojo.user.UserInfo;
import com.fruit.sdk.account.NoticeManager;
import com.fruit.service.sms.SmsCodeManager;
import com.fruit.service.sysmsg.SysMessageService;
import com.fruit.service.user.UserManager;
import com.fruit.util.ResultInfo;
import com.joe.utils.common.StringUtils;
import com.joe.utils.parse.json.JsonParser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 通知服务
 *
 * @author joe
 * @version 2017.12.12 10:06
 */
@Service
@Slf4j
public class NoticeService implements NoticeManager {
    private static final JsonParser PARSER = JsonParser.getInstance();
    @Autowired
    private SmsCodeManager smsCodeManager;
    @Autowired
    private SysMessageService sysMessageService;
    @Autowired
    private UserManager userManager;
    @Value("${h5_domain}")
    private String h5Domain;

    @Override
    public void allowCert(long uid) {
        log.debug("用户{}的实名认证通过，给该用户发送通知", uid);
        try {
            log.debug("发送实名认证成功短信");
            Map<String, Object> result = smsCodeManager.sendNoticeSmsToDB("realNameSuccess", getUserPhone(uid));
            log.debug("给用户{}发送实名认证短信结果为：{}", uid, result);
        } catch (Exception e) {
            log.warn("用户{}实名认证短信发送失败", uid, e);
        }
        try {
            log.debug("给用户{}发送实名认证通知", uid);
            String content = Templete.ALLOWCERT.toString();
            String title = MsgType.ALLOWCERT.toString();

            Message message = new Message();
            message.setContent(content);
            message.setTitle(title);
            message.setRedirectType("02");
            message.setRedirect("01");
            //2018.01.19  15:21   修改为最新的插入
            ResultInfo resultInfo = sysMessageService.insert((int) uid, "00", message, "00");
            log.debug("用户{}的实名认证通知结果为：{}", uid, PARSER.toJson(resultInfo));
        } catch (Exception e) {
            log.warn("给用户{}发送实名认证通知失败", uid, e);
        }
    }

    @Override
    public void rejectCert(long uid) {
        try {
            log.debug("发送实名认证失败短信");
            Map<String, Object> result = smsCodeManager.sendNoticeSmsToDB("realNameFail", getUserPhone(uid));
            log.debug("给用户{}发送实名认证失败短信结果为：{}", uid, result);
        } catch (Exception e) {
            log.warn("用户{}实名认证失败短信发送失败", uid, e);
        }
        try {
            log.debug("给用户{}发送实名认证被拒通知", uid);
            String content = Templete.REJECTCERT.toString();
            String title = MsgType.REJECTCERT.toString();
            Message message = new Message();
            message.setContent(content);
            message.setTitle(title);
            message.setRedirectType("01");
            message.setUrl(h5Domain + "/verified");
            //2018.01.19  15:21   修改为最新的插入
            ResultInfo resultInfo = sysMessageService.insert((int) uid, "00", message, "01");
            log.debug("用户{}的实名认证被拒通知结果为：{}", uid, PARSER.toJson(resultInfo));
        } catch (Exception e) {
            log.warn("给用户{}发送实名认证被拒通知失败", uid, e);
        }
    }

    @Override
    public void allowCashout(long uid, String way, String balance, String count) {
        try {
            log.debug("给用户{}发送提现通过通知，提现金额{}，钱包余额{}，提现方式{}", uid, count, balance, way);
            String content = Templete.ALLOWCASHOUT.toString();
            content = content.replace("${way}", way).replace("${balance}", balance).replace("${count}", count);
            String title = MsgType.ALLOWCASHOUT.toString();

            Message message = new Message();
            message.setContent(content);
            message.setTitle(title);
            message.setRedirectType("02");
            message.setRedirect("05");
            //2018.01.19  15:21   修改为最新的插入
            ResultInfo resultInfo = sysMessageService.insert((int) uid, "00", message, "04");
            log.debug("用户{}的提现通过通知结果为：{}", uid, PARSER.toJson(resultInfo));
        } catch (Exception e) {
            log.warn("给用户{}发送提现通过通知失败", uid, e);
        }
    }

    @Override
    public void rejectCashout(long uid, String count, String date, String cashMode, String cause) {
        try {
            log.debug("给用户{}发送提现被拒通知，提现金额{}，被拒日期{}，提现方式{}", uid, count, date, cashMode);
            String content = Templete.REJECTCASHOUTBYBANK.toString();
            content = content.replace("${count}", count).replace("${date}", date);
            String title = MsgType.REJECTCASHOUT.toString();

            Message message = new Message();
            message.setContent(content);
            message.setTitle(title);
            message.setRedirectType("02");
            message.setRedirect("05");
            //2018.01.19  15:21   修改为最新的插入
            ResultInfo resultInfo = sysMessageService.insert((int) uid, "00", message, "05");
            log.debug("用户{}的提现被拒通知结果为：{}", uid, PARSER.toJson(resultInfo));
        } catch (Exception e) {
            log.warn("给用户{}发送提现被拒通知失败", uid, e);
        }
    }

    @Override
    public String getUserPhone(long uid) {
        log.debug("用户{}获取手机号", uid);
        if (uid <= 0) {
            return null;
        }
        UserInfo userInfo = userManager.getUser((int) uid);
        log.debug("用户{}的手机号（用户信息）是：{}", uid, userInfo);
        if (userInfo == null || StringUtils.isEmpty(userInfo.getPhone())) {
            return null;
        } else {
            return userInfo.getPhone();
        }
    }

    @Override
    public String getUserHeader(long uid) {
        log.debug("用户{}获取头像", uid);
        if (uid <= 0) {
            return null;
        }
        UserInfo userInfo = userManager.getUser((int) uid);
        log.debug("用户{}的头像（用户信息）是：{}", uid, userInfo);
        if (userInfo == null || StringUtils.isEmpty(userInfo.getPhone())) {
            return null;
        } else {
            return userInfo.getHeadImage();
        }
    }
}
